<?php

namespace App\Admin\Controllers;

use App\Models\Meet;
use App\Models\Score;
use App\Models\Member;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class MeetsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('分享记录')
            ->description('分享时刻')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详细信息')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改信息')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建分享')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Meet);
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->id('Id');
        $grid->column('member.name','分享者');
        $grid->title('分享题目');
        $grid->share_time('分享时间');
        $grid->over_time('截止评分时间');
        $grid->column('平均分')->display(function (){
            $logs = Score::where('meet_id', $this->id)->get(['sum']);
            $list = [];
            foreach ($logs as $log)
            {
                array_push($list, $log->sum);
            }
            $length=count($list);
            if($length != 0) {
                $average = array_sum($list) / $length;
                return round($average, 2);
            }
        });
        $grid->column('二维码')->display(function () {
            $url='http://slave.mtaction.com/mtshare-web/score.html?meet_id='.$this->id.'?title='.$this->title.'?member_id='.$this->member_id.'?overtime='.$this->over_time.'';
            return '<a href="http://api.mtaction.com/dian/Qm/qrcode/qrcode.php?data='.$url.'"><button class="btn btn-info btn-sm">生成二维码</button></a>';
        });
        $grid->column('查看详请')->display(function () {
            return '<a href="scores?&meet_id='.$this->id.'"><button class="btn btn-info btn-sm">详细信息</button></a>';
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Meet::findOrFail($id));

        $show->id('Id');
        $show->name('分享者');
        $show->title('分享题目');
        $show->share_time('分享时间');
        $show->over_time('截止评分时间');
        $show->average('平均分');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Meet);

        $form->select('member_id', '请选择分享者')->options(Member::all()->pluck('name','id'));
        $form->text('title', '分享题目');
        $form->date('share_time', '分享时间')->default(date('H:i:s'));
        $form->date('over_time', '截止时间')->default(date('H:i:s'));

        return $form;
    }
}
